from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Chrome()
driver.get('https://www.12306.cn/index/')
id_word = 'fromStationText'
id_to_station_word = 'toStationText'
# 定位到12306的出发地
from_locator = (By.ID, id_word)
# 定位到12306的到达地
# to_locator = (By.ID, id_to_station_word)

# 强制等待10秒钟，如果没有返回就自动抛出异常
WebDriverWait(driver, 10).until(EC.presence_of_element_located(from_locator))
# 获取出发地输入框
input = driver.find_element_by_id(id_word)
# 清空输入框
input.clear()
input.send_keys("厦门北")
# 为了适应12306的查询机制，我们需要在搜索下拉菜单里去选择
# id = citem_0
driver.find_element_by_id('citem_0').click()

# WebDriverWait(driver, 10).until(EC.presence_of_element_located(to_locator))
# 获取目的地输入框
input = driver.find_element_by_id(id_to_station_word)
# 清空输入框
input.clear()
input.send_keys("黄山")
driver.find_element_by_id('citem_0').click()

# 定位到日期输入框，输入日期
date_id_word = 'train_date'
# date_locator = (By.ID, date_id_word)
# WebDriverWait(driver, 10).until(EC.presence_of_element_located(date_locator))
# 获取目的地输入框
input = driver.find_element_by_id(date_id_word)
# 清空输入框
input.clear()
input.send_keys("2023-05-01")

# 模拟点击查询按钮
# //*[@id="search_one"]
btn = driver.find_element_by_xpath('//*[@id="search_one"]')
btn.click()

time.sleep(5)
driver.close()